library(ggplot2) # install from hadley/ggplot2
library(RColorBrewer)
library(dplyr)
library(rgdal)
library(rgeos)
library(leaflet)
library(ggmap)  # install.packages("ggmap", type = "source")
  France <- readOGR(dsn=paste0(getwd(), "/datasets/maps"), layer="FRA_dept")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Fleur/Documents/OneDrive - AXA/05-Community/Training/DS4A Pricing Training 2017/Training/PricingTraining2017/Day2/DataViz/datasets/maps", layer: "FRA_dept"
## with 96 features
## It has 8 fields
## centroid coordinates of each regions
  centr <- gCentroid(France)@coords
  centr.Paris <- gCentroid(France[France@data$DEPT_ID==75,], byid = TRUE)@coords

## get map (require internet access)  
  frMap <- get_map(location = centr, zoom = 6)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=46.55819,2.549575&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false

Ploting dots (set of GPS) _______________________________________________________________________

  compet <- read.csv2(file = "./datasets/competitors.csv", stringsAsFactors = F)
  compet$competitor <- factor(compet$competitor)
  compet$lat <- as.numeric(compet$lat)
  compet$long <- as.numeric(compet$long)

  str(compet)
## 'data.frame':    5706 obs. of  6 variables:
##  $ competitor    : Factor w/ 3 levels "Allianz","AXA",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ adr.street    : chr  "281 AVENUE DU MARECHAL LECLERC" "2400 AVENUE DES MOULINS" "RESIDENCE BASTIO" "348 AVENUE DE PARIS" ...
##  $ adr.city      : chr  "91300 MASSY" "34080 MONTPELLIER" "20600 FURIANI" "79000 NIORT" ...
##  $ activity_label: chr  "Location de terrains et d'autres biens immobiliers" "Activit\x90\xa9s des agents et courtiers d'assurances" "Activit\x90\xa9s des agents et courtiers d'assurances" "Activit\x90\xa9s des agents et courtiers d'assurances" ...
##  $ lat           : num  48.7 43.6 42.7 46.3 48.2 ...
##  $ long          : num  2.3 3.83 9.43 -0.42 -3.97 ...

Using ggplot

If we take exactly the same previous example with polygon centroid instead of polygon itself : => use geom_point

In addition we can test it with more data points and plot a density :

## points
  ggmap(frMap) +
  geom_point(data = France@data, aes(x=long_ctd, y=lat_ctd, color = CR)) +
  scale_color_gradientn(colors = RColorBrewer::brewer.pal(5,"RdYlBu")[5:1])

## density
  ggmap(frMap) +
  geom_point(data = compet, aes(x=long, y=lat), position = "jitter", alpha = .5)
## Warning: Removed 113 rows containing missing values (geom_point).

  ggmap(frMap) +
  stat_density_2d(data = compet, aes(x=long, y=lat, fill = ..level..,  alpha =..level..), 
                  size = 1, bins = 16, geom = "polygon") +
  scale_fill_gradient(low = "green", high = "red") 
## Warning: Removed 113 rows containing non-finite values (stat_density2d).

Using Leaflet

Plotting GPS coordinates => rapidly feel the need of zoom/dezoom, and globally dynamic feature. A great javascript-based library for maps is leaflet :

  1. Create icon list
## your own icons
  competitor.ico <- iconList(
      AXA = makeIcon(iconUrl = "img/axa.png", iconWidth = 20, iconHeight = 20),
      Groupama = makeIcon(iconUrl = "img/groupama.png", iconWidth = 20, iconHeight = 20),
      Allianz = makeIcon(iconUrl = "img/allianz.png", iconWidth = 20, iconHeight = 20)
)

2a. Plot GPS on a map (using our own icons :) )

## simple dots
  leaflet(compet, options = leafletOptions(minZoom = 5, maxZoom = 18)) %>% 
    addTiles() %>%
    setView(centr.Paris[1], centr.Paris[2], zoom = 12) %>% 
    addMarkers(lng = ~long, lat = ~lat, icon = ~competitor.ico[competitor], 
               popup = ~as.character(paste(activity_label, "<br>", adr.street, "<br>", adr.city)))

2b. We can use js libraries like markercluster

## markercluster
  leaflet(compet, options = leafletOptions(minZoom = 5, maxZoom = 18)) %>% 
    addTiles() %>%
    setView(centr[1], centr[2], zoom = 5) %>% 
    addMarkers(clusterOptions = markerClusterOptions() )
## Assuming 'long' and 'lat' are longitude and latitude, respectively